﻿using System.Collections;

namespace SmsGui
{
    /// <summary>
    /// These extensions provides a convinient way for deserializing hash table's element from a JSON string. 
    /// </summary>
    static class JsonExtention
    {
        public static string GetStr(this System.Collections.Hashtable self, string key, string df)
        {
            string retval = df;
            if (self.Contains(key))
            {
                object obj = self[key];
                if (obj as string != null)
                {
                    retval = (string)obj;
                }
                else if (obj as double? != null)
                {
                    retval = ((double?)obj).ToString();
                }
                else if (obj as int? != null)
                {
                    retval = ((int?)obj).ToString();
                }
            }
            return retval;
        }
        public static string GetStr(this System.Collections.Hashtable self, string key)
        {
            return self.GetStr(key, string.Empty);
        }
        public static string GetString(this System.Collections.Hashtable self, string key, string df) // alias
        {
            return self.GetStr(key, df);
        }
        public static string GetString(this System.Collections.Hashtable self, string key) // alias
        {
            return self.GetStr(key);
        }

        public static double GetNum(this System.Collections.Hashtable self, string key, double df)
        {
            return self.Contains(key) ? (double)self[key] : df;
        }
        public static double GetNumber(this System.Collections.Hashtable self, string key, double df)
        {
            return self.Contains(key) ? (double)self[key] : df;
        }
        public static bool GetBoolean(this System.Collections.Hashtable self, string key, bool df) 
        {
            return self.Contains(key) ? (bool)self[key] : df;
        }

        public static Hashtable GetHash(this Hashtable self, string key)
        {
            return self.Contains(key) ? (Hashtable)self[key] : new Hashtable();
        }
        public static ArrayList GetArray(this Hashtable self, string key)
        {
            return self.Contains(key) ? (ArrayList)self[key] : new ArrayList();
        }
    }
}
